package com.example.notepadtxx;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.notepadtxx.database.MyHelper;
import com.example.notepadtxx.utils.DBUtils;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView note_back,note_save,note_delete;
    private TextView note_time,noteName;
    private EditText content;
    private String id;
    private MyHelper myHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        note_back = (ImageView)findViewById(R.id.note_back);
        note_save = (ImageView)findViewById(R.id.note_save);
        note_delete = (ImageView)findViewById(R.id.delete);
        note_time = (TextView)findViewById(R.id.tv_time);
        content = (EditText)findViewById(R.id.note_content);
        noteName = (TextView)findViewById(R.id.note_name);
        //给回退、删除、保存按钮注册监听
        note_back.setOnClickListener(this);
        note_save.setOnClickListener(this);
        note_delete.setOnClickListener(this);
        myHelper = new MyHelper(this);
        initData();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.note_back://“后退”按钮的点击事件
                finish();
                break;
            case R.id.delete://"清空"按钮的点击事件
                content.setText("");
                break;
            case R.id.note_save://"保存"按钮的点击事件
                //获取用户输入的内容
                String noteContent = content.getText().toString().trim();
                if(id !=null){//修改界面的数据
                    if(noteContent.length()>0){
                        if(myHelper.updateData(id,noteContent,DBUtils.getTime())){
                            showToast("修改成功");
                            setResult(2);
                            finish();
                        }else{
                            showToast("修改失败");
                        }
                    }else{
                        showToast("修改内容不能为空");
                    }
                }else {
                    //向数据库中添加数据
                    if (noteContent.length() > 0) {
                        if (myHelper.insertData(noteContent, DBUtils.getTime())) {
                            showToast("保存成功");
                            setResult(2);
                            finish();
                        } else {
                            showToast("保存失败");
                        }
                    } else {
                        showToast("输入内容不能为空！");
                    }
                }
        }
    }
    public void showToast(String message){
        Toast.makeText(RecordActivity.this,message,Toast.LENGTH_SHORT).show();
    }
    public void initData(){
        Intent intent = getIntent();
        if(intent != null){//将传来的内容初始化到界面上
            id = intent.getStringExtra("id");
            if(id!=null){
                noteName.setText("修改记录");
                content.setText(intent.getStringExtra("content"));
                note_time.setText(intent.getStringExtra("notetime"));
                note_time.setVisibility(View.VISIBLE);
            }
        }
        noteName.setText("添加记录");
    }
}
